package com.qyhl.webtv.module_circle.circle.msg.chat;


import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.user.ChatUserBean;
import com.qyhl.webtv.commonlib.entity.user.JsonChatUser;

import java.util.ArrayList;
import java.util.List;

import cn.leancloud.chatkit.LCChatKitUser;
import cn.leancloud.chatkit.LCChatProfileProvider;
import cn.leancloud.chatkit.LCChatProfilesCallBack;

/**
 * Created by helin on 2018/2/1.
 */

public class CustomUserProvider implements LCChatProfileProvider {


    private static CustomUserProvider customUserProvider;

    public synchronized static CustomUserProvider getInstance() {
        if (null == customUserProvider) {
            customUserProvider = new CustomUserProvider();
        }
        return customUserProvider;
    }

    private CustomUserProvider() {
    }


    @Override
    public void fetchProfiles(List<String> list, LCChatProfilesCallBack callBack) {
        //http://ucenter.i2863.com/interface?method=getUserInfo&username=13980974244&siteId=136 获取用户信息


        for (String userId : list) {
            getUserInfo(userId, list.size(), callBack);
        }
    }

    List<LCChatKitUser> userList = new ArrayList<LCChatKitUser>();

    /**
     * 获取用户信息
     *
     * @param userId
     */
    private void getUserInfo(final String userId, final int size, final LCChatProfilesCallBack callBack) {

        EasyHttp.post(CommonUtils.getInstance().getUserCenterUrl())
                .params("method", "getUserInfo")
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("username", userId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        userList.add(new LCChatKitUser(userId,"",""));
                        if( userList.size()==size ){
                            callBack.done(userList, null);
                        }
                    }

                    @Override
                    public void onSuccess(String s) {
                        JsonChatUser response = new Gson().fromJson(s,JsonChatUser.class);
                        if( response.getCode()==0 ){
                            ChatUserBean userData = response.getData();
                            userList.add(new LCChatKitUser(userData.getTelphone(),userData.getNickName(),userData.getLogo()));
                        }else{
                            userList.add(new LCChatKitUser(userId,"",""));
                        }
                        if( userList.size()==size ){
                            callBack.done(userList, null);
                            userList.clear();
                        }
                    }
                });
    }
}
